#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/11/11 下午1:57
# @Author  : yinxin
# @File    : spider_zhipin
# @Software: PyCharm


import inspect
import time

from common import delay
from common import detail_api_config, list_api_config
from log import hlog
from modules import Spider
from utils import crawl_list_url, crawl_detail_url, send_data, save_html, stop_spider
from utils.singleton import ListErrorCount, DetailErrorCount


class SpiderZhipin(Spider):

    def __init__(self, url, num, spider_path):

        func_name = "SpiderZhipin." + inspect.stack()[0][3]
        hlog.enter_func(func_name)

        self.primeval_url = url
        self.max_page = num
        self.spider_path = spider_path
        self.platform = "boss直聘"
        self.next = url

        hlog.var("SpiderZhipin.primeval_url", self.primeval_url)
        hlog.var("SpiderZhipin.max_page", self.max_page)
        hlog.var("SpiderZhipin.spider_path", self.spider_path)
        hlog.var("SpiderZhipin.platform", self.platform)
        hlog.var("SpiderZhipin.next", self.next)

        hlog.exit_func(func_name)

    def list_handle(self):

        func_name = "SpiderZhipin." + inspect.stack()[0][3]
        hlog.enter_func(func_name)

        i: int = 0
        while i < self.max_page and "" != self.next:
            i += 1
            result = crawl_list_url(self.spider_path["list"], self.next)
            self.next = result["next"]
            is_success = send_data(list_api_config, self.primeval_url, result["html"], self.platform)
            if not is_success:
                save_html("list", self.platform, self.primeval_url, result["html"])
                stop_spider("list", ListErrorCount.get_send_error_count(True))
            else:
                ListErrorCount.get_send_error_count()
            time.sleep(delay)

        hlog.exit_func(func_name)

    def detail_handle(self):

        func_name = "SpiderZhipin." + inspect.stack()[0][3]
        hlog.enter_func(func_name)

        result = crawl_detail_url(self.spider_path["detail"], self.next)
        is_success = send_data(detail_api_config, self.primeval_url, result, self.platform)
        if not is_success:
            save_html("detail", self.platform, self.primeval_url, result)
            stop_spider("detail", DetailErrorCount.get_send_error_count(True))
        else:
            DetailErrorCount.get_send_error_count()
        time.sleep(delay)

        hlog.exit_func(func_name)
